﻿using System;
using Tao.OpenGl;

namespace Aeon.Framework.Objects
{

/// <summary>
/// Axis cross-section primitive class.
/// Uses up to 3 colors, one for each axis.
/// 
/// </summary>
public class Cross : ObjectBase
    {
    // --- Attributes:
    
    private Single lenght;      // Axis half length.
    private Prism arrows;       // Axis arrows.
    
    // --- Constructor:

    /// <summary>
    /// Cross constructor.
    /// 
    /// </summary>
    /// <param name="key"> Unique identifier. </param>
    /// <param name="origin"> Object origin.  </param>
    /// <param name="angle"> Object orientation. </param>
    /// <param name="size"> Axis length. </param>
    public Cross(String key, Position origin, Orientation angle, Single size)
        : base(key,origin,angle,AeonMaterials.Default)
        {
        lenght = size/2;
        
        arrows = new Prism(key+"ArrowXYZ",Position.Origin,Orientation.Origin,lenght/8,lenght/15,0,10,AeonMaterials.Default);
        }

    // --- Public Methods:

    public override void Draw()
        {
        Gl.glTranslatef(origin.xPos,origin.yPos,origin.zPos);
        Gl.glRotatef(angle.xAng,1,0,0);
        Gl.glRotatef(angle.yAng,0,1,0);
        Gl.glRotatef(angle.zAng,0,0,1);
        
        // Draw Z:
        IColor color = new AeonColor(material.Color.R, material.Color.G, material.Color.B, material.Color.A);
        Gl.glColor4f(color.R,color.G,color.B,color.A);
        
        Gl.glBegin(Gl.GL_LINES);
        Gl.glNormal3i(0,1,0);
        
        Gl.glVertex3f(0, 0, -lenght);
        Gl.glVertex3f(0, 0, lenght);
        
        Gl.glEnd();
        
        // Z arrow:
        arrows.MoveTo(new Position(origin.xPos,origin.yPos,origin.zPos-lenght));
        arrows.RotateTo(new Orientation(angle.xAng-90,angle.yAng,angle.zAng));
        arrows.Material.Color = color;
        
        Gl.glPushMatrix();
        arrows.Draw();
        Gl.glPopMatrix();
        
        // Draw Y:
        color = new AeonColor(material.Color.R, material.Color.G, material.Color.B, material.Color.A);
        Gl.glColor4f(color.R,color.G,color.B,color.A);
        
        Gl.glBegin(Gl.GL_LINES);
        Gl.glNormal3i(0,0,-1);
        
        Gl.glVertex3f(0, -lenght, 0);
        Gl.glVertex3f(0, lenght, 0);
        
        Gl.glEnd();
        
        // Y arrow:
        arrows.MoveTo(new Position(origin.xPos,origin.yPos+lenght,origin.zPos));
        arrows.RotateTo(new Orientation(angle.xAng,angle.yAng,angle.zAng));
        arrows.Material.Color = color;
        
        Gl.glPushMatrix();
        arrows.Draw();
        Gl.glPopMatrix();
        
        // Draw X:
        color = new AeonColor(material.Color.R, material.Color.G, material.Color.B, material.Color.A);
        Gl.glColor4f(color.R,color.G,color.B,color.A);
        
        Gl.glBegin(Gl.GL_LINES);
        Gl.glNormal3i(0,1,0);
        
        Gl.glVertex3f(-lenght, 0, 0);
        Gl.glVertex3f(lenght, 0, 0);
        
        Gl.glEnd();
        
        // X arrow:
        arrows.MoveTo(new Position(origin.xPos+lenght,origin.yPos,origin.zPos));
        arrows.RotateTo(new Orientation(angle.xAng,angle.yAng+90,angle.zAng));
        arrows.Material.Color = color;
        
        Gl.glPushMatrix();
        arrows.Draw();
        Gl.glPopMatrix();
        }

    }


}
